package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserAddress;
import com.tr.rentcar.model.dao.UserAddressDao;


/**
 * Servlet implementation class UserAddress
 */
@WebServlet("/user/address")
public class UserAddresssController extends HttpServlet {
	UserAddressDao ua=new UserAddressDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user=(User) request.getSession().getAttribute("userinfo");
		String uaId=request.getParameter("uaid");
		if (uaId!=null) {
			try {
				if (ua.deleteByuaid(Integer.parseInt(uaId) )) {
				}
			}catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			//查询数据条数
			Long shu=ua.getAllCount(user.getUid());
			request.setAttribute("shu", shu);
			//查询数据
			List<UserAddress> usera = ua.findById(user.getUid());
			request.setAttribute("usera", usera);
			request.getRequestDispatcher("/WEB-INF/jsp/user_address.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String uaName=request.getParameter("uaName");
			String uaTel=request.getParameter("uaTel");
			String uaIdentity=request.getParameter("uaIdentity");
			String uaid=request.getParameter("uaid");
			User user=(User) request.getSession().getAttribute("userinfo");
			PrintWriter out=response.getWriter();
			try {
				UserAddress uas=new UserAddress();
				uas.setUaIdentity(uaIdentity);
				uas.setUaName(uaName);
				uas.setUaTel(uaTel);
				uas.setUid(user.getUid());
				//判断是否添加成功
				if (!StringUtils.isEmpty(uaid)) {
					int id=Integer.parseInt(uaid);
					out.println(ua.xgByUaid(id, uaName, uaTel, uaIdentity));
					return;
				}
				out.println(ua.save(uas));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
